package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.ServiceDTO;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.mapper.ServiceDetailMapper;
import com.carkeeper.mapper.ServiceMapper;
import com.carkeeper.pojo.ServiceDetailPO;
import com.carkeeper.pojo.ServicePO;
import com.carkeeper.service.ServiceService;
import com.carkeeper.vo.AddServiceVO;
import com.carkeeper.vo.ServiceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: dongxing
 * @date ：Created in 2020/5/25
 * @description ：服务业务层
 * @version: 1.0
 */
@Service
public class ServiceServiceImpl implements ServiceService {

    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private ServiceDetailMapper serviceDetailMapper;


    /**
     * 根据id查询服务详情
     *
     * @param id
     * @return
     */
    @Override
    public ServiceVO queryServiceById(Long id) {
        ServicePO servicePo = serviceMapper.findServiceById(id);
        ServiceDetailPO detailPo = serviceDetailMapper.selectById(id);
        if (servicePo == null) {
            throw new BussinessException("没有内容");
        }
        ServiceVO serviceVo = new ServiceVO();
        BeanUtils.copyProperties(servicePo, serviceVo);

        if (detailPo != null) {
            serviceVo.setInfo(detailPo.getInfo());
            serviceVo.setDetails(detailPo.getDetails());
        }
        serviceVo.setStore(servicePo.getStorePo());
        serviceVo.setServiceSubType(servicePo.getServiceSubTypePo());
        serviceVo.setServiceType(servicePo.getServiceTypePo());

        return serviceVo;

    }

    /**
     * 新增服务
     *
     * @param addServiceVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addService(AddServiceVO addServiceVo) {

        try {
            ServicePO servicePo = new ServicePO();
            BeanUtils.copyProperties(addServiceVo, servicePo);
            serviceMapper.insert(servicePo);
            ServiceDetailPO detailPo = new ServiceDetailPO();
            detailPo.setId(servicePo.getId());
            detailPo.setInfo(addServiceVo.getInfo());
            detailPo.setDetails(addServiceVo.getDetails());
            serviceDetailMapper.insert(detailPo);
            return true;
        } catch (Exception e) {
            throw e;
        }

    }

    /**
     * 服务项分页
     *
     * @param page
     * @param pageSize
     * @param code
     * @param name
     * @param subServiceTypeId
     * @param status
     * @param storeId
     * @return
     */
    @Override
    public Page<ServicePO> queryPage(Long page, Long pageSize,
                                     String code, String name,
                                     Long subServiceTypeId,
                                     Integer status, Long storeId) {
        Page<ServicePO> pageResult = new Page<>(page, pageSize);
        QueryWrapper<ServicePO> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(code)) {
            wrapper.lambda().like(ServicePO::getCode, code);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.lambda().like(ServicePO::getName, name);
        }
        if (subServiceTypeId != null && subServiceTypeId > 0) {
            wrapper.lambda().eq(ServicePO::getServiceSubTypeId, subServiceTypeId);
        }
        if (status != null) {
            wrapper.lambda().eq(ServicePO::getStatus, status);
        }
        if (storeId != null && storeId > 0) {
            wrapper.lambda().eq(ServicePO::getStoreId, storeId);
        }

        return serviceMapper.selectPage(pageResult, wrapper);
    }

    /**
     * 根据id删除服务项
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteService(Long id) {
        try {
            serviceMapper.deleteById(id);
            serviceDetailMapper.deleteById(id);
            return true;
        } catch (Exception e) {
            throw e;

        }
    }


    /**
     * 根据id更新服务项
     *
     * @param po
     * @return
     */

    @Override
    public boolean updateService(ServicePO po, String info, String details) {
        serviceMapper.updateById(po);
        if (!StringUtils.isEmpty(info) || !StringUtils.isEmpty(details)) {
            ServiceDetailPO detailPO = new ServiceDetailPO();
            detailPO.setId(po.getId());
            detailPO.setInfo(info);
            detailPO.setDetails(details);
            serviceDetailMapper.updateById(detailPO);
        }
        return true;
    }

    /**
     * 根据id查询列表
     *
     * @param ids
     * @return
     */
    @Override
    public List<ServiceDTO> getListById(List<Long> ids) {
        List<ServiceDTO> dtoList = new ArrayList<>();
        List<ServicePO> servicePos = serviceMapper.selectBatchIds(ids);
        for (ServicePO servicePo : servicePos) {
            ServiceDTO serviceDTO = new ServiceDTO();
            BeanUtils.copyProperties(servicePo, serviceDTO);
            dtoList.add(serviceDTO);
        }
        return dtoList;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public ServiceDTO getById(Long id) {
        ServiceDTO serviceDTO = new ServiceDTO();
        ServicePO servicePo = serviceMapper.selectById(id);
        if (servicePo == null) return null;
        BeanUtils.copyProperties(servicePo, serviceDTO);
        return serviceDTO;

    }


}
